package oa.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oa.entity.Department;
import oa.entity.Message;
import oa.paging.Page;
import oa.service.MessageService;
import oa.service.impl.MessageServiceImpl;

/**
 * Servlet implementation class Messageserviceselect
 */
@WebServlet("/messageserviceselect")
public class Messageserviceselect extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Messageserviceselect() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		MessageService me=new MessageServiceImpl();
		 List<Message> list = new ArrayList<Message>();
		HttpSession session = request.getSession();
		Page<Message> pageInfo = new Page<Message>();
		int counnts=0;
		int ComPanyId =0;
		String pageNo = request.getParameter("pageNo");			
		 if(pageNo==null){
				pageNo="1";
			}
		
		 try {
	
			int companyid=(int)session.getAttribute("companyid");
		    String companyids= request.getParameter("companyids");
		    pageInfo.setCurrentPage(Integer.parseInt(pageNo));
		    pageInfo.setPageSize(6);
		     if(companyid==0&&companyids==null){
		    	ComPanyId = companyid;
		    	list=me.selectMessages(companyid, pageInfo.getCurrentPage(), pageInfo.getPageSize());
		    	counnts=me.countMessage(companyid);
		    }else if(companyid==0&&companyids.equals("0")) {
		    	ComPanyId = Integer.parseInt(companyids);
		    	list=me.selectMessages(companyid, pageInfo.getCurrentPage(), pageInfo.getPageSize());
		    	counnts=me.countMessage(companyid);
		 
		    }
		     else if(companyid==0&&companyids!=null){
		    	ComPanyId = Integer.parseInt(companyids);
		    	list = me.selectMessages(Integer.parseInt(companyids),pageInfo.getCurrentPage(), pageInfo.getPageSize());
		        counnts=me.countMessage(Integer.parseInt(companyids));
		    }else if(companyid!=0&&companyids==null) {
		    	 ComPanyId = companyid;
		      	 list=me.selectMessages(companyid, pageInfo.getCurrentPage(), pageInfo.getPageSize());
		      	 counnts=me.countMessage(companyid);
		    }else if(companyid!=0&&companyids!=null) {
		    	 ComPanyId = Integer.parseInt(companyids);
		      	 list=me.selectMessages(Integer.parseInt(companyids), pageInfo.getCurrentPage(), pageInfo.getPageSize());
		      	 counnts=me.countMessage(companyid);
		   
		    }
		    pageInfo.setPageList(list);	
		    pageInfo.setTotalRow(counnts);	
		    session.setAttribute("OaCompanyId", ComPanyId);
			request.getSession().setAttribute("pageInfo", pageInfo);		
			request.getRequestDispatcher("message_list.jsp").forward(request, response);
		
		
			} catch (Exception e) {
				// TODO: handle exception
			}
		 }
		 


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
